import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:xvideos/hj_page/cartoon_business/acg_widget_item.dart';
import 'package:xvideos/hj_page/home_business/search_page/search_result_page/search_video_result_page.dart';
import 'package:xvideos/hj_utils/api_service/acg_service.dart';
import 'package:xvideos/tools_base/debug_log.dart';
import 'package:xvideos/tools_base/loading/l_error_widget.dart';
import 'package:xvideos/tools_base/loading/loading_center_widget.dart';

import '../../../../hj_model/acg/CartoonMoreList.dart';
import '../../../../hj_model/cartoon_media_info.dart';
import '../../../../tools_base/refresh/pull_refresh.dart';

class SearchACGResultPage extends StatefulWidget {
  final String keywords;
  final SearchType type;

  SearchACGResultPage({
    super.key,
    required this.type,
    required this.keywords,
  });

  @override
  State<SearchACGResultPage> createState() => _SearchACGResultPageState();
}

class _SearchACGResultPageState extends State<SearchACGResultPage> {
  RefreshController refreshController = RefreshController();
  int pageNumber = 1;
  List<CartoonMediaInfo>? videoList;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      _loadData();
    });
  }

  _loadData({int page = 1, int size = 12}) async {
    try {
      MediaSearchListModel? retResp =
          await ACGService.mediaSearch(keyword: widget.keywords, page: page, size: size, kind: widget.type.kind);
      pageNumber = page;
      videoList ??= [];
      if (page == 1) {
        videoList?.clear();
      }
      videoList?.addAll(retResp?.list ?? []);
      if (retResp?.hasNext == false) {
        refreshController.loadNoData();
      } else {
        refreshController.loadComplete();
      }
    } catch (e) {
      refreshController.loadComplete();
      debugLog(e);
    }
    refreshController.refreshCompleted();
    videoList ??= [];
    if (mounted) setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    if (videoList == null) {
      return LoadingCenterWidget();
    } else if (videoList?.isEmpty == true) {
      return CErrorWidget(
        retryOnTap: () {
          videoList = null;
          setState(() {});
          _loadData();
        },
      );
    } else {
      return pullYsRefresh(
        onInit: (ctr) => refreshController = ctr,
        onLoading: (ctr) => _loadData(page: pageNumber + 1),
        onRefresh: (ctr) => _loadData(),
        child: GridView.builder(
          padding: EdgeInsets.fromLTRB(12, 12, 12, 0),
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 3,
            mainAxisSpacing: 12,
            crossAxisSpacing: 6,
            childAspectRatio: 111 / 190,
          ),
          itemCount: videoList?.length ?? 0,
          itemBuilder: (context, index) {
            return AcgItemWidget(info: videoList![index]);
          },
        ),
      );
    }
  }
}
